﻿namespace Kit3D.Windows.Media
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Windows;

    public class PointCollection : Freezable, IList, IList<Point>
    {
        private List<Point> points;

        public PointCollection()
        {
            this.points = new List<Point>();
        }

        public PointCollection(int capacity)
        {
            this.points = new List<Point>(capacity);
        }

        public PointCollection(IEnumerable<Point> collection)
        {
            this.points = new List<Point>(collection);
        }

        #region ICollection<Point> Members

        public void Add(Point item)
        {
            Insert(Count, item);
        }

        public int Add(object value)
        {
            var point = (Point) value;
            Add(point);
            return IndexOf(point);
        }

        public bool Contains(object value)
        {
            return Contains((Point) value);
        }

        public void Clear()
        {
            this.points.Clear();
            OnChanged();
        }

        public int IndexOf(object value)
        {
            return IndexOf((Point) value);
        }

        public void Insert(int index, object value)
        {
            Insert(index, (Point) value);
        }

        public void Remove(object value)
        {
            Remove((Point) value);
        }

        public int IndexOf(Point item)
        {
            return this.points.IndexOf(item);
        }

        public void Insert(int index, Point item)
        {
            this.points.Insert(index, item);
            OnChanged();
        }

        void IList<Point>.RemoveAt(int index)
        {
            Remove(this[index]);
        }

        Point IList<Point>.this[int index]
        {
            get { return this[index]; }
            set { this[index] = value; }
        }

        void IList.RemoveAt(int index)
        {
            Remove(this[index]);
        }

        object IList.this[int index]
        {
            get { return this[index]; }
            set { this[index] = (Point)value; }
        }

        public bool Contains(Point item)
        {
            return this.points.Contains(item);
        }

        public void CopyTo(Point[] array, int arrayIndex)
        {
            this.points.CopyTo(array, arrayIndex);
        }

        public void CopyTo(Array array, int index)
        {
            CopyTo((Point[]) array, index);
        }

        public int Count
        {
            get { return this.points.Count; }
        }

        private object _syncObject = new object();
        public object SyncRoot
        {
            get { return _syncObject; }
        }

        public bool IsSynchronized
        {
            get { return true; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool IsFixedSize
        {
            get { return false; }
        }

        public bool Remove(Point item)
        {
            bool result = this.points.Remove(item);
            if (result)
            {
                OnChanged();
            }
            return result;
        }

        #endregion

        #region IEnumerable<Point> Members

        public IEnumerator<Point> GetEnumerator()
        {
            return this.points.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public Point this[int index]
        {
            get
            {
                return this.points[index];
            }
            set
            {
                if (index < Count)
                    Remove(this[index]);
                this.points[index] = value;
            }
        }

        #endregion
    }
}

